package com.imooc.controller;

import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "地址相关", tags = {"地址相关的api接口"})
@RequestMapping("address")
@RestController
public class AddressController {
    /**
     * 用户在确认订单页面,可以针对收货地址做如下操作
     * 1.查询用户所有的收货地址
     * 2.增加收货地址
     * 3.删~~~
     * 4.改~~
     * 5.设置默认的收货地址
     */

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "根据用户id查询用户收货地址列表", notes = "根据用户id查询用户收货地址列表", httpMethod = "POST")
    //这里定义post更多是基于隐私安全
    @PostMapping("/list")
    public IMOOCJSONResult list(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId) {
        if (userId == null) {
            return IMOOCJSONResult.errorMsg("");
        }
        List<UserAddress> list = addressService.queryAll(userId);
        //这里直接把list转给前端处理,并且在构造方法中我们可以看到他是封装到了data里,前端会处理这个数据
        return IMOOCJSONResult.ok(list);
    }

    @ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
    //这里定义post更多是基于隐私安全
    @PostMapping("/add")
    public IMOOCJSONResult add(
            @ApiParam(name = "addressBO", value = "地址业务对象", required = true)
            @RequestBody AddressBO addressBO) {
        IMOOCJSONResult checkRes = checkAddress(addressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }
        addressService.addNewUserAddress(addressBO);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户修改地址", notes = "用户修改地址", httpMethod = "POST")
    //这里定义post更多是基于隐私安全
    @PostMapping("/update")
    public IMOOCJSONResult update(
            @ApiParam(name = "addressBO", value = "地址业务对象", required = true)
            @RequestBody AddressBO addressBO) {

        //这里对传入对象BO进行addressId的判空
        if (StringUtils.isBlank(addressBO.getAddressId())) {
            return IMOOCJSONResult.errorMsg("修改地址错误: addressId不能为空");
        }
        IMOOCJSONResult checkRes = checkAddress(addressBO);
        if (checkRes.getStatus() != 200) {
            return checkRes;
        }
        addressService.updateUserAddress(addressBO);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户删除地址", notes = "用户删除地址", httpMethod = "POST")
    //这里定义post更多是基于隐私安全
    @PostMapping("/delete")
    public IMOOCJSONResult update(
            @ApiParam(name = "userId", value = "对应的用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "addressId", value = "对应数据库的addressId", required = true)
            @RequestParam String addressId) {

        //这里的判空可以防止恶意请求访问数据库
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return IMOOCJSONResult.errorMsg("");
        }
        addressService.deleteUserAddress(userId, addressId);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址", httpMethod = "POST")
    //这里定义post更多是基于隐私安全
    @PostMapping("/setDefalut")
    public IMOOCJSONResult setDefault(
            @ApiParam(name = "userId", value = "对应的用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "addressId", value = "对应数据库的addressId", required = true)
            @RequestParam String addressId) {

        //这里的判空可以防止恶意请求访问数据库
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return IMOOCJSONResult.errorMsg("");
        }
        addressService.updateUserAddressToBeDefault(userId, addressId);
        return IMOOCJSONResult.ok();
    }

    private IMOOCJSONResult checkAddress(AddressBO addressBO) {
        String receiver = addressBO.getReceiver();
        if (StringUtils.isBlank(receiver)) {
            return IMOOCJSONResult.errorMsg("收货人不能为空");
        }
        if (receiver.length() > 12) {
            return IMOOCJSONResult.errorMsg("收货人姓名不能太长");
        }

        String mobile = addressBO.getMobile();
        if (StringUtils.isBlank(mobile)) {
            return IMOOCJSONResult.errorMsg("收货人手机号不能为空");
        }
        if (mobile.length() != 11) {
            return IMOOCJSONResult.errorMsg("收货人手机号长度不正确");
        }
        boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
        if (!isMobileOk) {
            return IMOOCJSONResult.errorMsg("收货人手机号格式不正确");
        }

        String province = addressBO.getProvince();
        String city = addressBO.getCity();
        String district = addressBO.getDistrict();
        String detail = addressBO.getDetail();
        if (StringUtils.isBlank(province) ||
                StringUtils.isBlank(city) ||
                StringUtils.isBlank(district) ||
                StringUtils.isBlank(detail)) {
            return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
        }

        return IMOOCJSONResult.ok();
    }
}